package com.work.libtest2.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.work.libtest2.dto.EmployeeDto;
import com.work.libtest2.entity.Employee;
import com.work.libtest2.entity.R;
import com.work.libtest2.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.awt.print.Book;
import java.util.List;

/**
 * ClassName: UserController
 * Package: com.work.libtest2.controller
 * Description:
 *
 * @Author
 * @Create 2024/6/5 21:20
 * @Version 1.0
 */
@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {

    @Resource
    private EmployeeService employeeService;

    /**
     * 分页查找用户
     * */
    @GetMapping("{currentPage}/{pageSize}")
    public R getAllByPage(@PathVariable int currentPage, @PathVariable int pageSize, Employee employee){
        IPage<EmployeeDto> page = employeeService.getPage(currentPage, pageSize, employee);
        if (currentPage>page.getPages()){
            page=employeeService.getPage((int) page.getPages(), pageSize, employee);
        }
        return  new R(true,page);
    }
    /**
     *  删除用户
     * */
    @DeleteMapping("/{empId}")
    public R deleteEmployeeById(@PathVariable Integer empId){
        int i = employeeService.deleteEmpById(empId);
        if (i>0){
            return new R(true);
        }
        return new R(false);
    }
    /**
     * 新增员工操作
     * */
    @PostMapping
    public R save(@RequestBody Employee employee){
        int i = employeeService.saveEmployee(employee);
        return new R(i>0,i>0?"添加成功":"添加失败");
    }
    /**
     * 根据empId获取员工信息
     * */
    @GetMapping("/{empId}")
    public R getEmployeeById(@PathVariable Integer empId){
        Employee employee = employeeService.getEmployeeById(empId);
        return new R(true,employee);
    }
    /**
     * 修改员工信息
     * */
    @PutMapping
    public R updateEmployee(@RequestBody Employee employee){
        int i = employeeService.updateEmployeeById(employee);
        return new R(i>0);
    }
    /**
     * 获取所有的员工
     * */
    @GetMapping("/getAllEmpName")
    public R getAllEmpNaMe(){
        List<Employee> employeeList = employeeService.getAllEmpName();
        return new R(true,employeeList);
    }
}
